import { reactive } from 'vue'
import {MyExcel,exportExcelType} from './MyExcel'
import MyHttp from './MyHttp'
import MyWin from './MyWin'
import {IsInt,IsBigInt,IsByte,IsDate,toBool} from './TypeUtil'
export interface TableImportColumn{
  title: string, 
  key: string  ,
  type:string ,//string,byte ,int ,float,bool
  required?:boolean
}
export interface MySimpleImportOptions{
  file:any,
  columns:Array<TableImportColumn> ,
  defaultObj:any
}
 
// exceljs相关方法
export class MySimpleImport {
   
  public async importExcel(options: MySimpleImportOptions): Promise<any > {
    const { 
      file, 
      columns,
      defaultObj  } = options;

      const importData= async (blob:Blob,columns:Array<TableImportColumn>,
        defaultObj:any
        ):Promise<any >=>{
          let excel=new MyExcel(blob);
          let header=new Array();
          columns.forEach(Element=>{
            header.push(Element.title);
          })
          let l=await excel.importExcel({
              i:1,
              header:header
          });
           
          let row0=l[0];
          var filedErr="";
          header.forEach(element => {
              if(element!=row0[element]){
                filedErr+=element+",";
              }
          });
          if(filedErr!=""){
            MyWin.alert("字段不匹配 "+filedErr,{});
            return new Promise((resolver,reject) => reject(true));
          }

          let rowsErr="";
          let lr=new Array();
          for(let i=1;i<l.length;i++){
               const dstobj = Object.assign({}, defaultObj);
              var mr=    Object.assign({}, defaultObj) ;
              let element =l[i];
              type ObjectKey = keyof typeof mr;
              let rowErr="";
              
              columns.forEach(col => {
                if(col.required){
                  if(!element[col.title]){
                    rowErr=`第${i} 行 ${col.title} 不能为空`; 
                    return;
                  }
                }
                if(!element[col.title]){
                  return;
                }
                
                switch(col.type)
                {
                  case "int" :
                    {
                      if(!IsInt(element[col.title])){
                        rowErr=`第${i} 行 ${col.title} 格式错误`; 
                        return;
                      }
                      mr[col.key]=parseInt(element[col.title]);
                    } 
                    break;
                  case "byte" :
                  {
                    
                    if(!IsByte(element[col.title])){
                      rowErr=`第${i} 行 ${col.title} 格式错误`; 
                      return;
                    }
                    mr[col.key]=parseInt(element[col.title]);
                  } 
                  break;
                  case "number":                                      
                  {
                    if(!IsInt(element[col.title])){
                      rowErr=`第${i} 行 ${col.title} 格式错误`; 
                      return;
                    }
                    mr[col.key]=parseInt(element[col.title]);

                  }
                    break;
                    case "date":                                      
                    {
                      
                      if(!IsDate(element[col.title])){
                        rowErr=`第${i} 行 ${col.title} 格式错误`; 
                        return;
                      }
                      mr[col.key]=element[col.title];
  
                    }
                      break;
                  case "bool" :
                    {                       
                      mr[col.key]=toBool(element[col.title]);
                    } 
                    break;
                  default:
                    { 
                        if(element[col.title])
                        {                          
                          mr[col.key]=element[col.title];
                        }
                    }
                    break;
                } 
               
            });

            if(rowErr!=""){
              rowsErr+=rowErr+"\r\n";
            } else{
              lr.push(mr);
            }         
          }
          if(rowsErr!=""){
            MyWin.alert(rowsErr,{});
            return new Promise((resolver,reject) => reject(true));
          }
          return new Promise((resolver) => resolver(lr));
      }
      var blob=await this.readFile(file);
      return await  importData(blob,columns,defaultObj);
       
    }
     
    public  async readFile(file:any): Promise<Blob> {
      return new Promise<Blob>((resolve, reject) => {
        const reader = new FileReader();
        reader.onloadend =    function(r) { 
          const blob = new Blob(
            [reader.result as ArrayBuffer], 
            { type: file.type });
           
            return resolve(blob);
        }
        reader.readAsArrayBuffer(file);     
 
   
      });
    }
 
  }
   